<?php
class CacheFile{
	private static $cacher = null;
	private $cacheDir;
    private $outTime;
    private $outDel;
	private function __construct($config){
//        $config=[
//            "type"=>"file",
//            "dir"=>"/",
//              "outtime"=>"300"
//        ];
		$this->cacheDir=str_replace(["/","\\"],DIRECTORY_SEPARATOR,$config["dir"]);
        $this->outTime=@$config["outtime"]?$config["outtime"]:3600;
        $this->outDel=@$config["outdel"]?true:false;
        if($this->cacheDir[strlen($this->cacheDir)-1]!=DIRECTORY_SEPARATOR){
            $this->cacheDir=$this->cacheDir.DIRECTORY_SEPARATOR;
        }
        createDir($this->cacheDir);
	}

	public static function new($conf){
        if(!$conf){
            $conf=".cache";
        }
        if(function_exists("Config")){
            $conf=Config($conf);
        }else{
            throw new Exception("缓存配置错误");
        }
		if(self::$cacher == null){self::$cacher = new CacheFile($conf);}
		return self::$cacher;
	}
    public static function newc($conf){
        if(self::$cacher == null){self::$cacher = new CacheFile($conf);}
        return self::$cacher;
    }
	public function get($name){
		$cacheFile = $this->cacheDir.$this->getname($name).'.php';
		if(!is_file($cacheFile)){return false;}
        if(!defined("CacheFile")){define("CacheFile",true);}
		$cacheData = require $cacheFile;
        $cacheData= unserialize($cacheData);
		if($cacheData['expire'] < time()){
            if($this->outDel){
                $this->removeCache($name);
            }
            return null;
        }
		return $cacheData['data'];
	}

	public function set($name, $val, $expire=null){
		$cacheFile = $this->cacheDir.$this->getname($name).'.php';
        $pinfo=pathinfo($cacheFile);
        if(isset($pinfo["dirname"])){
            createDir($pinfo["dirname"]);
        }
        if($val===null){
            $this->remove($name);
            return;
        }
		$cacheContent = '<?php
if(!defined("CacheFile")){exit();}
$data = <<<EOF
';
		$cacheData = array(
			'data'   => $val,
			'expire' => time() + ($expire?$expire:$this->outTime)
		);
		$cacheData = str_replace('\\', '\\\\', serialize($cacheData));
		$cacheData = str_replace('$', '\$', $cacheData);
		$cacheContent .=  $cacheData.'
EOF;
return $data;';
		file_put_contents($cacheFile, $cacheContent);
	}

	public function remove($name){
		$cacheFile = $this->cacheDir.$this->getname($name).'.php';
		if(!is_file($cacheFile)){return true;}
		unlink($cacheFile);
		return true;
	}

	public function clear(){
		$files = scandir($this->cacheDir);
		foreach($files as $v){
			if($v != '.' && $v != '..'){
				$cacheUrl = $this->cacheDir.$v;
				if(is_file($cacheUrl)){
					@unlink($cacheUrl);
				}
			}
		}
		return true;
	}

	public function close(){

	}
    private function createDir($path){
        if (!file_exists($path)){
            $this->createDir(dirname($path));
            if(!@mkdir($path, 0777)) {
                die(dirname($path).' permission denied');
            }
        }
    }
    private function getname($name){
        return str_replace(["\\","/"],DIRECTORY_SEPARATOR,$name);
    }
}